<template>
  <div class="page-container padding-20 query-list-container">
    <div v-show="$route.name === 'roleMgt'">
      <div class="toolbar-container">
        <el-button type="primary" icon="el-icon-plus" size="small" @click="linkToAdd">新建</el-button>
      </div>
      <el-table class="mt-20 el-table-modify" :data="tableData" border>
        <el-table-column prop="id" label="序号" align="center" width="50"></el-table-column>
        <el-table-column prop="name" align="center" label="角色"></el-table-column>
        <el-table-column prop="empQuantity" align="center" label="员工数量"></el-table-column>
        <el-table-column prop="remark" align="center" label="备注"></el-table-column>
        <el-table-column align="center" label="权限配置" width="140">
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="linkToEdit(scope.row)">编辑</el-button>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" fixed="right" width="140">
          <template slot-scope="scope">
            <!-- <el-button size="mini" type="text" @click="linkToEdit(scope.row)">修改</el-button> -->
            <!-- <el-button size="mini" type="text" @click="deleteRole(scope.row)">删除</el-button> -->
            <el-button size="mini" type="text" @click="linkToAddUser(scope.row)">新增</el-button>
            <el-button size="mini" type="text" @click="linkToUserMgt(scope.row)">管理</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        ref="pagination"
        class="mt-20"
        background
        :page-size.sync="pageOption.pageSize"
        :total="pageOption.total"
        @size-change="onSizeChange"
        @current-change="onPageChange"
        layout="slot,->,prev,pager,next,sizes,jumper"
      >
        <span class="pagination-title">
          共 {{ pageOption.total }} 条记录 第 {{ pageOption.page}} /
          {{ pageOption.pageTotal }} 页
        </span>
      </el-pagination>
    </div>
    <router-view v-if="$route.name !== 'roleMgt'"
      :editData="selectData" @refresh="requestData"></router-view>
  </div>
</template>

<script>
import { rolesQuery, roleDelete } from "@/api/roles";

export default {
  name: "RoleMgt",
  data() {
    return {
      tableData: [],
      pageOption: {
        page: 1,
        pageSize: 20,
        total: 0,
        pageTotal: 1
      },
      selectData: null
    };
  },
  methods: {
    onSearch() {
      this.pageOption.page = 1;
      this.requestData();
    },
    onReset() {
      this.pageOption.page = 1;
      this.requestData();
    },
    linkToAdd() {
      this.selectData = null;
      this.$router.push({ name: "addRole" });
    },
    linkToEdit(row) {
      this.selectData = row;
      this.$router.push({ name: "editRole"});
    },
    linkToAddUser(row) {
      this.selectData = null;
      this.$router.push({ name: "addUser", query: {roleId: row.id} });
    },
    linkToUserMgt(row) {
      this.$router.push({
        name: "userMgt",
        query: {roleId: row.id} 
      });
    },
    async deleteRole(row) {
      try {
        this.$confirm('确定删除该角色吗？', {
          type: 'warning',
          callback: async (action) => {
            if (action == 'confirm') {
              await roleDelete(row.id);
              this.requestData();
            }
          }
        });
      } catch (err) {}
    },
    async requestData() {
      const { page, pageSize: size } = this.pageOption;
      const { data, total } = await rolesQuery({ page: page - 1, size });
      let pageTotal = Math.ceil(total / size);
      this.tableData = data;
      this.pageOption.total = total;
      this.pageOption.pageTotal = pageTotal || 1;

      if (this.$refs.pagination) {
        let childrenRefs = this.$refs.pagination.$children;
        this.$nextTick(() => {
          childrenRefs.forEach(child => child.$forceUpdate());
        });
      }
    },
    onSizeChange(size) {
      this.pageOption.pageSize = size;
      this.requestData();
    },
    onPageChange(page) {
      this.pageOption.page = page;
      this.requestData();
    }
  },
  created() {
    this.requestData();
  }
};
</script>

<style lang="scss">
.pagination-title {
  font-weight: normal;
}
</style>
